#!/bin/bash
########################################################
#
# Script Name:				configchk.sh
# Description:				Check server configuration
# Output:					/tmp/configchk.out
#
# Created:					12/22/10
# Updated:					12/22/10 - 15:41EST
#
# Modified By:				nandrews
#
########################################################

# Set Initial Variables
OUTFILE=/tmp/configchk.out

# Start our output file. If it exists already, toss it
if [ -f $OUTFILE ]
then
	rm $OUTFILE
fi

echo "######################################################"	>> $OUTFILE
echo "# configchk.out"											>> $OUTFILE
echo "# Configuration check tool for ec2host control panel"		>> $OUTFILE
echo "#"														>> $OUTFILE
echo "# All output marked ERROR must be fixed before"			>> $OUTFILE
echo "# proceeding with installation."							>> $OUTFILE
echo "#"														>> $OUTFILE
echo "# It is recommended, but not required to fix any"			>> $OUTFILE
echo "# output marked WARNING. These issues may still have"		>> $OUTFILE
echo "# a negative impact on control panel functionality,"		>> $OUTFILE
echo "# and should be fixed if at all possible."				>> $OUTFILE
echo "######################################################"	>> $OUTFILE

# First, gather some system information
if [ `cat /etc/issue | awk '{print $1}'` == "Ubuntu" ]
then
	DISTRO=Ubuntu
	VERSION=`cat /etc/issue | awk '{print $2}' | awk 'BEGIN{FS="."}{print $1}'`
else if [ `cat /etc/fedora-release | awk '{print $1}'` == "Fedora" ]
then
	DISTRO=Fedora
	VERSION=`cat /etc/fedora-release | awk '{print $3}'`
else
	# If we get here, we're running on an unsupported version.
	# Alert and error in the output file.
	echo "######################################################"	>> $OUTFILE
	echo "# OS Check"												>> $OUTFILE
	echo "ERROR - Unsupported distribution"							>> $OUTFILE
	echo "     ec2host currently only supports Fedora and Ubuntu"	>> $OUTFILE
	echo "######################################################"	>> $OUTFILE
	
	# exit non-zero
	echo "ERROR - Unsupported distribution. Check $OUTFILE for details."
	exit 1
fi

# Put a success message in the output file
echo "######################################################"	>> $OUTFILE
echo "# OS Check"												>> $OUTFILE
echo "SUCCESS - $DISTRO $VERSION found."						>> $OUTFILE
echo "######################################################"	>> $OUTFILE


# Make sure the version of either distro is up to snuff
VCHECK=0
UBUMIN=8
FEDMIN=12

if [ $DISTRO == "Ubuntu" ]
then
	if [ $VERSION < $UBUMIN ]
	then
		$VCHECK=1
	fi
else if [ $DISTRO == "Fedora" ]
then
	if [ $VERSION < $FEDMIN ]
	then
		$VCHECK=1
	fi
fi

if [ $VCHECK == 1 ]
then
	# Drop a failure message to the output file
	echo "######################################################"	>> $OUTFILE
	echo "# Version Check"											>> $OUTFILE
	echo "ERROR - Unsupported OS Version"							>> $OUTFILE
	echo "     You are running $DISTRO $VERSION"					>> $OUTFILE
	echo "     ec2 host requires a minimum version of:"				>> $OUTFILE
	if [ $DISTRO == "Ubuntu" ]
	then
		echo "      $DISTRO $UBUMIN"								>> $OUTFILE
	else
		echo "      $DISTRO $FEDMIN"								>> $OUTFILE
	fi
fi